package com.fs.base.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fs.base.jpa.specification.Criteria;
import com.fs.base.pojo.Org;
import com.fs.base.pojo.User;
import com.fs.base.repository.OrgRepository;
import com.fs.base.utils.ApplicationUtils;
import com.fs.base.vo.Combotree;
import com.fs.base.vo.JsonResponse;
/**
 * OrgController
 * @author ACE
 *
 */
@Controller
@RequestMapping("/console/org/rewrite")
public class OrgController {
	private static final Logger log = Logger.getLogger(OrgController.class);
	@Autowired
	private OrgRepository orgRepository;
	private List<Org> orgs = new ArrayList<Org>();
	
	@ResponseBody
	@RequestMapping("/tree")
	public Object tree() {
		try {
			Sort sort = new Sort(Sort.Direction.ASC,"createTime");//排序
			Criteria<Org> c = new Criteria<Org>();
			//User user = ApplicationUtils.User();
			//c.addAuto("createrId", user.getId(), "eq");//添加过滤
			orgs = orgRepository.findAll(c, sort);
			//orgs.add(new Org("1","组织管理","0",1));
			Combotree tree = new Combotree();
			tree.setIdField("id");
			tree.setPidField("parentId");
			tree.setTextField("orgName");
			Combotree combotree = Combotree.builder(orgs, tree);
			return combotree.getData();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return JsonResponse.error(e.getMessage());
		}
	}
}
